ARD2  RC2
Airbag Reference Demonstrator using MPC5604P
Deployment.c
Go to the documentation of this file.
00001 
00016 #include "derivative.h"
00017 #include "Compile_Options.h"
00018 #include "Deployment.h"
00019 #include "SM.h"
00020 #include "SBC_AL.h"
00021 #include "HAL.h"
00022 #include "MailScheduler.h"
00023 #include "Application_Globals.h"
00024 #include "SIU.h"
00025 #ifdef USE_LABVIEW
00026 #include "Labview.h"
00027 #endif
00028 /*
00029  ******************************************************************************
00030  * Constants
00031  ******************************************************************************
00032  */
00034 const uint16_t cau16PreDeploymentValidStates[] =
00035 {
00036   SM_STATE_ERROR, SM_STATE_DECISION };
00037 /*
00038  ******************************************************************************
00039  * Globals
00040  ******************************************************************************
00041  */
00042 /*
00043  ******************************************************************************
00044  * u32fnStateDeployment
00045  ******************************************************************************
00046  */
00047 uint32_t u32fnStateDeployment(void)
00048 {
00049   uint32_t u32Status;
00050   uint16_t u16TrueDeploymentSquibs;
00051   uint8_t u8NextState;
00052   
00053   u8NextState = CLEAR;
00054   
00055   /* Verify that we can execute - Previous state ended correctly */
00056   u32Status
00057       = u32fnSMValidateCurrentState((uint16_t*)cau16PreDeploymentValidStates,
00058                                     N_ELEMENTS(cau16PreDeploymentValidStates));
00059   if(CLEAR == u32Status)
00060   {
00061     /* Here comes the real contents of the state */
00062     LOCK_STATE_EXECUTION();
00063     
00064     /* Figure out which squibs we really need to fire since we don't */
00065     /* want to have refiring */
00066     u16TrueDeploymentSquibs = (uint16_t)(gu16SquibsToFire & ~gu16FiredSquibs);
00067     
00068     /* Here below lies the most dangerous line in all of the project */
00069     /* This call fires squibs */
00070     u32Status = u32fnSBCFireSquibs(u16TrueDeploymentSquibs);
00071     
00072     if(CLEAR == u32Status)
00073     {
00074       /* Next we start figuring out what just happened */
00075       u32Status = u32fnDeploymentStatusReport(u16TrueDeploymentSquibs,
00076                                               (uint16_t*)&gu16FiredSquibs,
00077                                               (uint8_t*)&u8NextState);
00078       
00079       /* Only continue if there was a deployment or lack of it */
00080       if(CLEAR != u32Status)
00081       {
00082         /*-- CAN messages and others will go here --*/
00083       }
00084       else
00085       {
00086         /* We can leave now */
00087       }
00088     }
00089     else
00090     {
00091       /* Get out with the current error */
00092     }
00093     
00094   }
00095   else
00096   {
00097     /* Leave */
00098   }
00099 
00100   /* Set the next state */
00101   if(CLEAR == u32Status)
00102   {
00103     vfnSMWriteNextState(u8NextState);
00104   }
00105   else
00106   {
00107     vfnSMWriteNextState(SM_STATE_ERROR);
00108   }
00109   
00110   /* Unlock the state */
00111   UNLOCK_STATE_EXECUTION();
00112   return (u32Status);
00113 }
00114 /*
00115  ******************************************************************************
00116  * u32fnDeploymentStatusReport
00117  ******************************************************************************
00118  */
00119 uint32_t u32fnDeploymentStatusReport(const uint16_t cu16ExpectedFiring,
00120                                      uint16_t* pu16FiredSquibs,
00121                                      uint8_t* pu8NextState)
00122 {
00123   /* Declare locals */
00124   uint32_t u32Status;
00125   uint16_t u16FiredSquibsCopy;
00126 #ifdef USE_LABVIEW
00127   uint8_t u8LabviewStatus;
00128 #endif
00129   
00130   /* Init locals */
00131   u16FiredSquibsCopy = *pu16FiredSquibs;
00132   u32Status = CLEAR;
00133   
00134   /* We need to figure out what just happened. First, collect which   */
00135   /* Squibs were fired, if any.                                       */
00136   u32Status = u32fnSBCSquibFiringStatus(pu16FiredSquibs);
00137   
00138   if(CLEAR == u32Status)
00139   { 
00140     /* Were all selected channels fired? */
00141     u32Status = (uint32_t)((cu16ExpectedFiring & ~(*pu16FiredSquibs)));
00142     
00143     /* Check if at least one squib was fired */
00144     if(CLEAR == *pu16FiredSquibs)
00145     {
00146       /* Nothing happened - We can leave */
00147       *pu8NextState =  SM_STATE_GUI;
00148     }
00149     else
00150     {
00151       /* If there was at least one fired squib, we need to act accordingly */
00152       /* Turn on the warning lamp */
00153       (void)u8fnSBCEnableWarningLamp(TRUE);
00154 
00155       /* Remove the satellite from the list of active satellites */
00156       gu16ActivePSI5Channels &= ~gu16ConfirmedSatellites;
00157       
00158       /* Do the same thing for the squibs */
00159       gu16ActiveSquibChannels &= ~(*pu16FiredSquibs);
00160       
00161 #ifdef USE_LABVIEW
00162       vfnGUISendDeploymentEvent();
00163 #endif
00164       
00165       /* Depending on whether we have some functionality left or not, */
00166       /* we will decide were to go next                               */
00167       if((CLEAR < gu16ActiveSquibChannels) & (CLEAR < gu16ActivePSI5Channels))
00168       {
00169         /* Jump directly to Acquisition state */
00170         *pu8NextState = SM_APPLICATION_START;
00171       }
00172       else
00173       {
00174         /* We can't possibly do anything else. Flag it as an error and exit */
00175         u32Status = DEPLOYMENT_ERR_SAT_AND_SQUIBS_DEPLOYED;
00176         *pu8NextState = SM_STATE_ERROR;
00177       }
00178     }
00179   }
00180   else
00181   {
00182     /* Get out */
00183   }
00184   
00185   /* Remember all fired squibs, even those from previous occasions */
00186   *pu16FiredSquibs |= u16FiredSquibsCopy;
00187   
00188   return(u32Status);
00189 }
00190 #ifdef USE_LABVIEW
00191 /* This section is for demo purposes only and should be removed in a */
00192 /* real application */
00193 void vfnGUISendDeploymentEvent(void)
00194 {
00195   uint8_t u8LabviewStatus;
00196   uint16_t au16LabviewData[6u];
00197   
00198   /* We will enable Serial communication */
00199   vfnLVEnable(TRUE);
00200   
00201   /* Construct a new message */
00202   au16LabviewData[0u] = gau16RawAccels[0u];
00203   au16LabviewData[1u] = gau16RawAccels[3u];
00204   au16LabviewData[2u] = gau16RawAccels[6u];
00205   au16LabviewData[3u] = gau16RawAccels[9u];
00206   au16LabviewData[4u] = gau16RawAccels[12u];
00207   au16LabviewData[5u] = gau16RawAccels[13u];
00208   
00209   /* And wait for any on-going transmission to finish */
00210   u8LabviewStatus = u8fnLVWaitForTxPort();
00211   
00212   /* Send latest acceleration */
00213   vfnDisplaySCIData(LV_DA_MSG, LV_16, 6u, 1u,
00214                    (uint8_t*)au16LabviewData);
00215   
00216   /* And send event */
00217   vfnLVTriggerEvent();
00218   return;
00219 }
00220 #endif
00221 /*
00222  ******************************************************************************
00223  *
00224  *  End of file.
00225  *
00226  ******************************************************************************
00227  */